# Sync Script: Move matrix_html content to yamhk.github.io (preserving .git directory)

param(
    [string]$SourcePath = "C:\Users\Administrator\workspace\demo\matrix_html",
    [string]$DestinationPath = "C:\Users\Administrator\workspace\demo_ot\yamhk.github.io"
)

Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "Starting sync process..." -ForegroundColor Cyan
Write-Host "========================================= " -ForegroundColor Cyan

# Check if source directory exists
if (-not (Test-Path $SourcePath)) {
    Write-Error "Source directory does not exist: $SourcePath"
    exit 1
}

# Check if destination directory exists
if (-not (Test-Path $DestinationPath)) {
    Write-Error "Destination directory does not exist: $DestinationPath"
    exit 1
}

Write-Host "Cleaning destination directory ($DestinationPath), preserving .git..." -ForegroundColor Yellow

# Remove all files and folders in destination except .git
Get-ChildItem -Path $DestinationPath -Exclude ".git" | Remove-Item -Recurse -Force

Write-Host "Copying files from source ($SourcePath) to destination..." -ForegroundColor Yellow

# Copy all files from source to destination (excluding .git)
$robocopyResult = Robocopy.exe $SourcePath $DestinationPath /MIR /XD ".git" /NFL /NDL /NJH /NJS

# Robocopy exit codes (https://ss64.com/nt/robocopy-exit.html)
# 0 - No errors occurred, and no copying was done (files are identical)
# 1 - One or more files were copied successfully
# 2 - Some Extra files or directories were detected (not fatal)
# 3 - Something happened while copying (fatal)
# 4-7 - Various fatal errors (access denied, etc.)
# 8 - Some files or directories could not be deleted (non-fatal)

if ($LASTEXITCODE -lt 8) {
    Write-Host "Sync completed successfully! Robocopy exit code: $LASTEXITCODE" -ForegroundColor Green
} else {
    Write-Error "Error occurred during sync, robocopy exit code: $LASTEXITCODE"
    exit $LASTEXITCODE
}